package com.elink.enginerring.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elink.enginerring.co.BaseCo;
import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.domain.Praise;
import com.elink.enginerring.domain.Reply;
import com.elink.enginerring.service.ReplyService;
import com.elink.enginerring.util.JSONUtil;

@RestController
@RequestMapping("/api/reply")
@Api(value="百问百答回复接口")
public class ReplyController {
	@Autowired
	private ReplyService replyService;
	
	/**
	 * 添加回复
	 * @param reply
	 * @return
	 */
	@RequestMapping(value = "/addReply", method = RequestMethod.POST)
	@ApiOperation(value = "添加回复", httpMethod="POST", notes = "添加回复")
	public Map<String, Object> addReply(@ModelAttribute @Valid Reply reply,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>)request.getAttribute("userInfo");
		if(userInfo != null && userInfo.containsKey("userId")){
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够回复
				String userId =  userInfo.get("userId").toString();
				reply.setUserId(userId);
				
				return replyService.addReply(reply);
			}else{
				return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR);
			}
		}
		return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR); 
	}
	
	/**
	 * 修改回复
	 * @param reply
	 * @return
	 */
	@RequestMapping(value = "/updateReply", method = RequestMethod.POST)
	@ApiOperation(value = "修改回复", httpMethod="POST", notes = "对我的回复进行修改")
	public Map<String, Object>updateReply(@ModelAttribute @Valid Reply reply,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>)request.getAttribute("userInfo");
		if(userInfo != null && userInfo.containsKey("userId")){
			if((int)userInfo.get("userType")==Constant.personUserType){//只有个人用户才能够回复
				String userId =  userInfo.get("userId").toString();
				reply.setUserId(userId);
				return replyService.updateReply(reply);
			}else{
				return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR);
			}
		}
		return JSONUtil.getJSON(ErrorType.AUTHORITY_ERROR); 

	}
	
	/**
	 * 评选最佳回复
	 * @param replyId
	 * @return
	 */
	@RequestMapping(value = "/updateSelect", method = RequestMethod.GET)
	@ApiOperation(value = "评选最佳回复", httpMethod="GET", notes = "评选最佳回复")
	public Map<String, Object> updateSelect(@ApiParam(value="回复的主键",required=true)
	@RequestParam(value="replyId",required=true)int replyId){
		return replyService.updateSelect(replyId);
	}
	
	/**
	 * 获取我回复的问题
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getReplyById", method = RequestMethod.GET)
	@ApiOperation(value = "获取我回复的问题", httpMethod="GET", notes = "分页获取获取我回复的问题")
	public Map<String, Object> getReplyById(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return replyService.getReplyById(co,userId);
	}
	
	/**
	 * 回复点赞/喝倒彩
	 * @param praise
	 * @return
	 */
	@RequestMapping(value = "/addPraise", method = RequestMethod.POST)
	@ApiOperation(value = "点赞/喝倒彩)" , httpMethod="POST", notes = "点赞/喝倒彩")
	public Map<String, Object> addPraise(@ModelAttribute @Valid Praise praise,HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		praise.setUserId(userId);
		return replyService.addPraise(praise);
	}
}
